<?php
  include('header.php');
  $hide_footer = -1;
  if (isset($_SESSION['signed_in']) and $guest_access == '0') {
      header('location: signin.php');
  } else {
  if(!is_numeric($_GET['f'])) {
  header('location: index.php');
  }
  $userid = -5;
  if (isset($_SESSION['user_id']))
  {$userid = $_SESSION['user_id'];}
      $bc = $db->query("
SELECT 
 cat_name, 
 cat_id, 
 cat_permission 
FROM " . $table_prefix . "categories 
WHERE 
cat_id =" . mysql_real_escape_string($_GET['f']));

      if (!$bc) {
          header('location: index.php');
      } else {
          $bcrow = $db->fetch_array($bc,'assoc');
          if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] >= 1) {
              $hide_forum = 0;
          }
          if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] < 1) {
              $pageTitle = $l_error_permission;
              // Get all the page's HTML into a string
              $pageContents = ob_get_contents();
              // Wipe the buffer 
              ob_end_clean();
              echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
              echo "<hr><br /><div align='center'>$l_error_permission<br /><br /> <a href = 'index.php'>$l_back_to</a></div><br /><br />";
              die;
          }
          if ($bcrow['cat_permission'] == 3 and $_SESSION['signed_in'] == false) {
              $pageTitle = "$l_error_permission";
              // Get all the page's HTML into a string
              $pageContents = ob_get_contents();
              // Wipe the buffer 
              ob_end_clean();
              echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
              echo "<hr><br /><div align='center'>$l_error_permission<br /><br /> <a href = 'index.php'>$l_back_to</a></div><br /><br />";
              die;
          }
          if ($bcrow['cat_permission'] == 2 and $_SESSION['signed_in'] == false) {
              $pageTitle = $l_error_permission;
              // Get all the page's HTML into a string
              $pageContents = ob_get_contents();
              // Wipe the buffer 
              ob_end_clean();
              echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
              echo "<hr><br /><div align='center'>$l_error_permission<br /><br /> <a href = 'index.php'>$l_back_to</a></div><br /><br />";
              die;
          }
          if ($bcrow['cat_permission'] == 2 and $_SESSION['signed_in']) {
              $hide_forum = 0;
          }
          if ($bcrow['cat_permission'] <= 1) {
              $hide_forum = 0;
          }
          
          if (mysql_real_escape_string($_GET['f']) != $bcrow['cat_id']) {
              header('location: index.php');
          }
          $pageTitle = $bcrow['cat_name'];
          $qqq = 0;
		  
		  $check_approved = "AND topic_status <= 1";
			  if ($_SESSION['user_level']>=1)
			  {$check_approved = "";}
		  
		  $result = $db->query("
SELECT COUNT(*) 
FROM " . $table_prefix . "topics 
WHERE 
topic_cat=" . mysql_real_escape_string($_GET['f']) . " $check_approved"); $qqq += 1;
          if (!$result) {
              echo $l_error_occured;
          } else {
              echo "<table><tr>";
              //BREADCRUMBS  
              if ($breadcrumbs == '1') {
                  if (mysql_real_escape_string($_GET['f']) != "") {
                      $tags = array('{FORUM}', '{SEPARATOR}', '{F_ID}', '{CAT_NAME}', '{SEPARATOR2}', '{T_SUBJECT}', );
                      $data = array('Forum', $l_breadcrumb_separator, mysql_real_escape_string($_GET['f']), $bcrow['cat_name'], '', '');
                      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/breadcrumbs.html"));
                  }
              }
              //END BREADCRUMBS 
			  $r = $db->fetch_row($result);
              $numrows = $r[0];
              // number of rows to show per page
              $rowsperpage = $topics_per_page;
              $totalpages = ceil($numrows / $rowsperpage);
              if (isset($_GET['page']) && is_numeric($_GET['page'])) {
                  $currentpage = (int)$_GET['page'];
              } else {
                  $currentpage = 1;
              }
              if ($currentpage > $totalpages) {
                  $currentpage = $totalpages;
              }
              if ($currentpage < 1) {
                  $currentpage = 1;
              }
              if ($totalpages <= 1) {
                  $totalpages = 1;
              }

              $offset = ($currentpage - 1) * $rowsperpage;
              if ($current_page == "") {
                  $current_page = 1;
              }
              $range = $max_pagination_link;
              if ($range >= $totalpages) {
                  $range = $totalpages;
              }
              echo '<td align="right" class="pagination"> <b>'.$l_page.' ' . $current_page . ' '.$l_page_of.' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
              if ($currentpage > 1) {
                  echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=1'><<</a> ";
                  $prevpage = $currentpage - 1;
                  echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=$prevpage'><</a> ";
              }
              for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                  if (($x > 0) && ($x <= $totalpages)) {
                      if ($x == $currentpage) {
                          echo " [<b>$x</b>] ";
                      } else {
                          echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=$x'>$x</a> ";
                      }
                  }
              }
              if ($currentpage != $totalpages) {
                  $nextpage = $currentpage + 1;
                  echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=$topicid&page=$nextpage'>></a> ";
                  echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=$topicid&page=$totalpages'>>></a> ";
              }
              echo '</td></tr></table>';

			  $result = $db->query("
  SELECT
topic_id,
topic_subject,
topic_date,
topic_cat,
topic_by,
view,
reply,
topic_starter,
topic_last_poster_id,
topic_last_poster,
topic_last_post_date,
topic_status,
topic_sort,
topic_icon FROM " . $table_prefix . "topics 
  WHERE topic_cat = " . mysql_real_escape_string($_GET['f']) . " 
  $check_approved ORDER BY 
  topic_sort,
  topic_last_post_date DESC LIMIT $offset, $rowsperpage") or die(mysql_error()); $qqq += 1;
              if (!$result) {
                  echo "An error occured, please try again later.";
              } else {
                  $cat_icon = '*';
                  $tags = array('{ICON}', '{TOPIC}', '{REPLIES}', '{VIEWS}', '{CREATED}', );
                  $data = array($cat_icon, $l_topic, $l_replies, $l_views, $l_created);
                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/topics.html"));
              }
              
              while ($list = $db->fetch_array($result,'assoc')) {
                  $pages = ceil(($list['reply']) / $post_per_page);
                  $last_post = $list['reply'];
                  $last_post += 1;
                  if ($pages == 0) {
                      $pages += 1;
                  }
                  $last_poster = "<a class='last_poster' href = 'user.php?u=" . $list['topic_last_poster_id'] . "'>" . $list['topic_last_poster'] . "</a>";
                  if ($list['topic_last_poster_id'] <= 1) {
                      $last_poster = "<i>" . $list['topic_last_poster'] . "</i>";
                  }
                  //TOPIC ICONS
                  $topic_icon = "./style/" . $default_style . "/img/normal_post.gif";
                  $title_icon = "$l_thot";
                  
                  if ($list['reply'] >= $hot_topics_post) {
                      $topic_icon = "./style/" . $default_style . "/img/topic_hot.gif";
                      $title_icon = "$l_tnormal";
                  }
                  
                  if ($list['topic_by'] == $userid) {
                      $topic_icon = "./style/" . $default_style . "/img/my_topic.gif";
                      $title_icon = "$l_tyou";
                  }
                  
                  if ($list['topic_by'] == $userid and $list['reply'] >= $hot_topics_post) {
                      $topic_icon = "./style/" . $default_style . "/img/my_hot_topic.gif";
                      $title_icon = "$l_thotyou ";
                  }
                  
                  if ($list['topic_status'] == 1) {
                      $topic_icon = "./style/" . $default_style . "/img/topic_locked.gif";
                      $title_icon = "$l_tlocked";
                  }
                  $latest = "./style/" . $default_style . "/img/";
                  $td_class = "";
                  if ($list['topic_sort'] == -1) {
                      $topic_icon = "./style/" . $default_style . "/img/quick_sticky.gif";
                      $title_icon = "$l_tsticky ";
					  $td_class = " class='sticky'";
                  }
                  $latest = "./style/" . $default_style . "/img/";
                  
                  if ($list['topic_sort'] == -1 and $list['topic_status'] == 1) {
                      $topic_icon = "./style/" . $default_style . "/img/normal_post_locked_sticky.gif";
                      $title_icon = "$l_tlockedsticky";
					  $td_class = " class='sticky'";
                  }
                  $latest = "./style/" . $default_style . "/img/";
                  
                  //END TOPIC ICONS
                  $tpages = ceil(($list['reply'] + 1) / $post_per_page);
                  if ($tpages <= 1) {
                      $topic_pages = "";
                  } else {
                      $topic_pages = " (<img src = './style/$default_style/img/more_pages_view.gif'> <a class='underline' href = 'topic.php?t=$list[topic_id]&page=1'>1</a>";
                      $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=2'>2</a>";
                      if ($tpages > 2) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=3'>3</a>";
                      }
                      if ($tpages > 3) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=4'>4</a>";
                      }
                      if ($tpages > 4) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=5'>5</a>";
                      }
                      if ($tpages > 5) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=6'>6</a>";
                      }
                      if ($tpages > 6) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=7'>7</a>";
                      }
                      if ($tpages > 7) {
                          $topic_pages .= " <a class='underline' href = 'topic.php?t=$list[topic_id]&page=8'>8</a>";
                      }
                      
                      if ($tpages > 8) {
                          $topic_pages .= " ... <a class='underline' href = 'topic.php?t=$list[topic_id]&page=$pages'>last page</a>";
                      }
                      $topic_pages .= ")";
                  }
                  if ($list['topic_icon'] != "" or $list['topic_icon'] != "0") {
                      $t_icon = "<img align='absmiddle' src='./img/misc/" . $list['topic_icon'] . "'>";
                  }
                  
                  if ($list['topic_icon'] == "" or $list['topic_icon'] == "0") {
                      $t_icon = "";
                  }
                  
                  $topic_author = "<a href='user.php?u=" . $list['topic_by'] . "'>" . $list['topic_starter'] . "</a>";
				  //POST DATE
				  $post_date = date($date_format, strtotime($list['topic_last_post_date']));
				  
				  if (date("d.m.Y", strtotime($list['topic_last_post_date'])) == $date->format("d.m.Y")) {
				  $post_date = "<b>$l_today</b>, " . date("H:i:s", strtotime($list['topic_last_post_date']));
				  }
				  if (date("d.m.Y", strtotime($list['topic_last_post_date'])) == date("d.m.Y",$yesterday)) {
                  $post_date = "<b>$l_yesterday</b>, " . date("H:i", strtotime($list['topic_last_post_date'])) . "";
                   }  
				  
                  $tags = array('{F_ID}', '{T_ID}', '(TOPIC_SUBJECT)', '{TOPIC_AUTHOR}', '{REPLY}', '{VIEW}', '{T_DATE}', '{LAST_POSTER}', '{PAGE}', '{LAST_POST}', '{IMG_PATH}', '{TITLE}', '{SMALL_ICON}', '{PAGES}', '{TOPIC_ICON}','{VIEW_LATEST}','{STICKY}','{BY}');
                  $data = array(mysql_real_escape_string($_GET['f']), $list['topic_id'], trim($list['topic_subject']), $topic_author, $list['reply'], $list['view'], $post_date, $last_poster, $pages, $last_post, $topic_icon, $title_icon, $latest, $topic_pages, $t_icon,$l_view_latest,$td_class,$l_by);
                  echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/topics_row.html"));
              }
              echo '</table>';
              echo "<table><tr>";
              //BREADCRUMBS  
              if ($breadcrumbs == '1') {
                  if (mysql_real_escape_string($_GET['f']) != "") {
                      $tags = array('{FORUM}', '{SEPARATOR}', '{F_ID}', '{CAT_NAME}', '{SEPARATOR2}', '{T_SUBJECT}', );
                      $data = array('Forum', $l_breadcrumb_separator, mysql_real_escape_string($_GET['f']), $bcrow['cat_name'], '', '');
                      echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/breadcrumbs.html"));
                  }
              }
              //END BREADCRUMBS 
              
              //PAGINATION BOTTOM
              if (isset($_GET['page']) && is_numeric($_GET['page'])) {
                  $currentpage = (int)$_GET['page'];
              } else {
                  $currentpage = 1;
              }
              if ($currentpage > $totalpages) {
                  $currentpage = $totalpages;
              }
              if ($currentpage < 1) {
                  $currentpage = 1;
              }
              $offset = ($currentpage - 1) * $rowsperpage;
              if ($current_page == "") {
                  $current_page = 1;
              }
              $range = $max_pagination_link;
              if ($range >= $totalpages) {
                  $range = $totalpages;
              }
              echo '<td align="right" class="pagination"> <b>'.$l_page.' ' . $current_page . ' '.$l_page_of.' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
              if ($currentpage > 1) {
                  echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=1'><<</a> ";
                  $prevpage = $currentpage - 1;
                  echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=$prevpage'><</a> ";
              }
              for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                  if (($x > 0) && ($x <= $totalpages)) {
                      if ($x == $currentpage) {
                          echo " [<b>$x</b>] ";
                      } else {
                          echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?f=" . mysql_real_escape_string($_GET['f']) . "&page=$x'>$x</a> ";
                      }
                  }
              }
              if ($currentpage != $totalpages) {
                  $nextpage = $currentpage + 1;
                  echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?t=$topicid&page=$nextpage'>></a> ";
                  echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?t=$topicid&page=$totalpages'>>></a> ";
              }
              echo '</td></tr></table>';
              //END PAGINATION BOTTOM  
              if ($show_totals_on_forums == "1") {
                  $cat_icon = '<img border="0" src = "./style/' . $default_style . '/img/forum_icon.png" alt="" align="middle">';
                  $topic_icon = "<img border='0' src = './style/" . $default_style . "/img/normal_post.gif' alt='' align='middle'>";
                  
                  echo '<br>&nbsp;<a style="text-decoration:none;" class="menuButtons" href="create_topic.php?f=' . mysql_real_escape_string($_GET['f']) . '">' . $l_new_topic . '</a><br>';
                  echo "<br />" . $topic_icon . " $l_total_topics " . $numrows;
              }
              echo '<br /><br /><table style="width:250px;height:120px;border: 1px solid #BBBBBB;margin-left:17px;" border="1"><tr><td>
<img border="0" src = "./style/' . $default_style . '/img/my_topic.gif" alt="" align="middle"> ' . $l_tyou . '<br/>
<img border="0" src = "./style/' . $default_style . '/img/normal_post.gif" alt="" align="middle"> ' . $l_tnormal . '<br/>
<img border="0" src = "./style/' . $default_style . '/img/quick_sticky.gif" alt="" align="middle" width="20px" height="19px"> ' . $l_tsticky . '<br/>
<img border="0" src = "./style/' . $default_style . '/img/topic_hot.gif" alt="" align="middle"> ' . $l_hotmore . '<br/>
<img border="0" src = "./style/' . $default_style . '/img/topic_locked.gif" alt="" align="middle" width="20px" height="19px"> ' . $l_tlocked . '<br/>
</td></tr></table>
';
          }
      }
  }
  if ($hide_footer < 1) {
      include 'footer.php';
  }
  // Get all the page's HTML into a string
  $pageContents = ob_get_contents();
  // Wipe the buffer 
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>